You can quickly determine if a text includes a specific pattern using Regex. There are multiple ways to work with Regex in PowerShell.
#Sample text $text = @" This is (a) sample text, this is a (sample text) "@ #Sample pattern: Content wrapped in () $pattern = '\(.*?\)'
To determine if a string matches a pattern using the built-in -matches operator, use the syntax 'input' -match 'pattern'. This will return true or false depending on the result of the search. If there was match you can view the match and groups (if defined in pattern) by accessing the $Matches-variable.
> $text -match $pattern True > $Matches Name Value ---- ----- 0 (a)
You can also use -match to filter through an array of strings and only return the strings containing a match.
> $textarray = @" This is (a) sample text, this is a (sample text) "@ -split "`n" > $textarray -match $pattern This is (a) sample a (sample text) Version ≥ 2.0
PowerShell 2.0 introduced a new cmdlet for searching through text using regex. It returns a MatchInfo object per textinput that contains a match. You can access it's properties to find matching groups etc.
> $m = Select-String -InputObject $text -Pattern $pattern > $m This is (a) sample text, this is a (sample text) > $m | Format-List * IgnoreCase : True LineNumber : 1 Line : This is (a) sample text, this is a (sample text) Filename : InputStream Path : InputStream Pattern : \(.*?\) Context : Matches : {(a)}
Like -match, Select-String can also be used to filter through an array of strings by piping an array to it. It creates a MatchInfo-object per string that includes a match.
> $textarray | Select-String -Pattern $pattern This is (a) sample a (sample text) #You can also access the matches, groups etc. > $textarray | Select-String -Pattern $pattern | fl * IgnoreCase : True LineNumber : 1 Line : This is (a) sample Filename : InputStream Path : InputStream Pattern : \(.*?\) Context : Matches : {(a)} IgnoreCase : True LineNumber : 3 Line : a (sample text) Filename : InputStream Path : InputStream Pattern : \(.*?\) Context : Matches : {(sample text)}
Select-String can also search using a normal text-pattern (no regex) by adding the -SimpleMatch switch.
You can also use the static Match() method available in the .NET [RegEx]-class.
> [regex]::Match($text,$pattern) Groups : {(a)} Success : True Captures : {(a)} Index : 8 Length : 3 Value : (a) > [regex]::Match($text,$pattern) | Select-Object -ExpandProperty Value (a)
A common task for regex is to replace text that matches a pattern with a new value.
#Sample text $text = @" This is (a) sample text, this is a (sample text) "@ #Sample pattern: Text wrapped in () $pattern = '\(.*?\)' #Replace matches with: $newvalue = 'test'
The -replace operator in PowerShell can be used to replace text matching a pattern with a new value using the syntax 'input' -replace 'pattern', 'newvalue'.
> $text -replace $pattern, $newvalue This is test sample text, this is a test
Replacing matches can also be done using the Replace() method in the [RegEx] .NET class.
[regex]::Replace($text, $pattern, 'test') This is test sample text, this is a test
A regex-pattern uses many special characters to describe a pattern. Ex., . means "any character", + is "one or more" etc.
To use these characters, as a .,+ etc., in a pattern, you need to escape them to remove their special meaning. This is done by using the escape character which is a backslash \ in regex. Example: To search for +, you would use the pattern \+.
It can be hard to remember all special characters in regex, so to escape every special character in a string you want to search for, you could use the [RegEx]::Escape("input") method.
> [regex]::Escape("(foo)") \(foo\) > [regex]::Escape("1+1.2=2.2") 1\+1\.2=2\.2